<?php
/**
 * Created by PhpStorm.
 * User: Dinh Thi
 * Date: 1/19/15
 * Time: 1:52 PM
 */

namespace DinhThi\MVC;

class Request {
    private static $instance = null;
    private $url = '';
    private $params = array();
    private function __construct(){
    }

    public function init(){
        if (isset ($_GET['url'])){
            $this->url      = $_GET['url'];
            $this->params   = explode('/', $_GET['url']);
        }
    }

    /**
     * @return Request
     */
    public static function getInstance(){
        if (Request::$instance == null){
            Request::$instance = new Request();
        }
        return Request::$instance;
    }

    /**
     * Get all POST request
     * @return null
     */
    public function getPosts(){
        if (isset ($_POST)){
            return $_POST;
        }
        return null;
    }

    /**
     * Get all GET request
     * @return null
     */
    public function getParameters(){
        if (isset ($_GET)){
            return $_GET;
        }
        return null;
    }

    /**
     * Get POST with index = $name
     * If not found then return $defaultValue
     * @param $name
     * @param null $defaultValue
     * @return null
     */
    public function getPost($name, $defaultValue = null){
        if (isset ($_POST[$name]))
            return $_POST[$name];
        return $defaultValue;
    }

    /**
     * Get GET with index = $name
     * If not found then return $defaultValue
     * @param $name
     * @param null $defaultValue
     * @return null
     */
    public function getParameter($name, $defaultValue = null){
        if (isset ($_GET[$name]))
            return $_GET[$name];
        return $defaultValue;
    }


    /**
     * Return list of url array
     * @return array
     */
    public function getParams(){
        return $this->params;
    }

    /**
     * Get full url
     * @return mixed
     */
    public function getURL(){
        return $_GET['url'];
    }



} 